<?php
	class Migration_1_1_1 extends GigaMigration
	{
		function migrate($migrateShell)
		{
            $this->createOrderTable($migrateShell);
			$this->alterListingTable($migrateShell);
            $this->alterUserTable($migrateShell);
            $this->alterReviewTable($migrateShell);
            $this->alterEav($migrateShell);
			$this->addNewEmailTemplates($migrateShell);
		}
        
        protected function alterUserTable($migrateShell)
        {
             $UserModel  = cri("Users.User");
            if($UserModel->hasField('business_information') == false)
            {
                $UserModel->query("ALTER TABLE `$UserModel->table` ADD `business_information` TEXT NOT NULL");
            }
        }
        protected function alterListingTable($migrateShell)
        {
            $Listing = cri("Listing");
            if($Listing->hasField('owner_id') == false)
            {
                $Listing->query("ALTER TABLE `$Listing->table` ADD `owner_id` INT NULL");
            }
            if($Listing->hasField('owner_candidate_id') == false)
            {
                $Listing->query("ALTER TABLE `$Listing->table` ADD `owner_candidate_id` INT NOT NULL");
            }
        }
        protected function createOrderTable($migrateShell)
        {
          $orderTable = "orders";
          App::import("Core","Schema");
          $SchemaObject = new CakeSchema();
          $db =& ConnectionManager::getDataSource($SchemaObject->connection);
          $db->fullDebug = true;
          $NewSchema = $SchemaObject->load();
          $OldSchema = $SchemaObject->read(array("models"=>false));
          $compare = @$SchemaObject->compare($OldSchema, $NewSchema);
          //prd($compare['orders']);
          if(!isset($compare[$orderTable]))
          {
            return;
          }
          $ordersTableSql = $db->alterSchema(array($orderTable => $compare[$orderTable]));
          if($ordersTableSql)
          {
            $migrateShell->out("Making changes to order table");
            if(!$db->_execute($ordersTableSql))
            {
               $migrateShell->out("Error in updating $orderTable");
               $migrateShell->out("Error : ".$db->lastError());
            }else
            {
              $migrateShell->out("$orderTable table updated");
            }
          }
          $db->_execute("DROP TABLE IF EXISTS `order_items`");
        }
		
		protected function alterReviewTable()
		{
            App::import("Model","Review");
			$Review = cri("Review");
			if($Review->hasField('listing_asset_count') == false)
			{
				$Review->query("ALTER TABLE `$Review->table` ADD `listing_asset_count` INT NOT NULL");
			}
            if($Review->hasField('published_email_sent') == false)
            {
              $Review->query("ALTER TABLE `$Review->table` ADD `published_email_sent` TINYINT NOT NULL");
              $Review->recursive = -1;
              $Review->updateAll(array("published_email_sent"=>1),array("status"=>Review::PUBLISHED));
            }
		}
		
		protected function alterEav()
		{
            $CustomField = cri("CustomField");
            $CustomField->query("ALTER TABLE `$CustomField->table` ADD `other_information` TEXT NOT NULL");
			App::import("Model","Eav.EavValue");
			$IntModel = new EavValuesInt();
			$StringModel = new EavValuesString();
			$ArrayModel = new EavValuesArray();
			$TextModel = new EavValuesText();
			$FloatModel = new EavValuesFloat();
			$DateModel = new EavValuesDate();
			$DatetimeModel = new EavValuesDatetime();
			foreach(array($IntModel,$ArrayModel,$TextModel,$FloatModel,$DateModel,$DatetimeModel) as $ValueModel)
			{
				$ValueModel->query("ALTER TABLE `$ValueModel->table` CHANGE `field` `field` VARCHAR(250)");
			}
			//$query = 
		}
		protected function createIndexes()
		{
			$Listing = cri("Listing");
			$Listing->query("ALTER TABLE `$Listing->table` ADD INDEX ( `created` ) ");
		}
		protected function addNewEmailTemplates()
		{
			$EmailTemplate = cri("Cms.EmailTemplate");
			$templates = array_keys(cr('emailConfig.Buttonset'));
			foreach($templates as $templateName)
			{
                if($EmailTemplate->hasAny(array('name'=>$templateName)))
                {
                  continue;
                }
				$fileName = 'template_'.$templateName.'.ctp';
				$filePath = WWW_ROOT.'files'.DS.$fileName;
				if(!file_exists($filePath))
				{
					continue;
				}
				echo $templateName."\n";	
				$mailSubject = Configure::read('emailConfig.MailConfiguration.subject_'.$templateName);
				if(is_null($mailSubject))
				{
					$mailSubject = '';
				}
				$content = file_get_contents($filePath);
				$id = $EmailTemplate->field('id',array('name'=>$templateName));
				$data['EmailTemplate'] =  array('body_text'=>$content,'body_html'=>$content,'subject'=>$mailSubject,'name'=>$templateName);
				$EmailTemplate->create();
				if($id)
				{
					$EmailTemplate->id = $id;
				}
				$EmailTemplate->save($data);
			}
		}
	}
?>